package web.adminUI;

import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import web.sharedUI.ErrorMessage;
import web.sharedUI.SessionBean;
import ejb.contentManagement.AirportMgr;
import ejb.dto.AirportDTO;


@ManagedBean
@RequestScoped
public class AirportBean {
	
	public AirportBean() {
		airport = new AirportDTO();
		setErrorMessage(null);
	}
	
	@EJB
	private AirportMgr airportMgr; 
	
	@SuppressWarnings("unused")
	private List<AirportDTO> allAirports;
	
	private int airportId;
	private AirportDTO airport;
	private String errorMessage;
	
	/**
	 * Delete Airport from database
	 * @param id
	 * @return redirect to page <code>manageAirports</code>
	 */
	public String deleteAirport(int id) {
		boolean result = airportMgr.deleteAirport(id);
		if(!result){
			SessionBean.displayError(ErrorMessage._DELETE);
		}
		return "/admins/manageAirports?faces-redirect=true";
	}
	
	/**
	 * Update Airport in database
	 * @return redirect to page <code>manageAirports</code>
	 */
	public String updateAirport() {
		boolean result = airportMgr.updateAirport(airport);
		if(!result){
			SessionBean.displayError(ErrorMessage._UPDATE);
			return null;
		}
		return "/admins/manageAirports?faces-redirect=true";
	}
	
	/**
	 * Insert Airport in database
	 * @return redirect to page <code>manageAirports</code>
	 */
	public String insertAirport() {
		boolean result = airportMgr.insertAirport(airport);
		if(!result){
			SessionBean.displayError(ErrorMessage._INSERT);
			return null;
		}
		return "/admins/manageAirports?faces-redirect=true";
	}
	
	/*
	 * GETTERS & SETTERS
	 */
	
	public List<AirportDTO> getAllAirports() {
		return airportMgr.getAllAirportsDTO();
	}

	public void setAllAirports(List<AirportDTO> allAirports) {
		this.allAirports = allAirports;
	}


	public int getAirportId() {
		return airportId;
	}

	public void setAirportId(int airportId) {
		this.airportId = airportId;
		airport = airportMgr.getAirportDTO(airportId);
		if(airport == null)
			SessionBean.notFound();
	}

	public AirportDTO getAirport() {
		return airport;
	}

	public void setAirport(AirportDTO airport) {
		this.airport = airport;
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}	

	
}
